package acwing.闫氏DP分析法;
import java.util.*;

public class 最长公共子序列{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n,m;
        n = sc.nextInt();
        m = sc.nextInt();
        String a,b;
        int[][] f = new int[n+1][m+1];
        a=sc.next();b=sc.next();
        char[] aa = new char[n+1];
        char[] bb = new char[m+1];

        for(int i=1;i<=n;i++)
        {
            aa[i]=a.charAt(i-1);
        }
        for(int i=1;i<=m;i++){
            bb[i]=b.charAt(i-1);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                f[i][j]=Math.max(f[i-1][j],f[i][j-1]);
                if(aa[i] == bb[j]){
                    f[i][j]=Math.max(f[i][j],f[i-1][j-1]+1);
                }
            }
    sc.close();
    System.out.println(f[n][m]);
        
    }
}